﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.ServiceModel;
using System.ServiceModel.Activation;
using System.ServiceModel.Web;
using System.Text;
using System.IO;
using System.Web;
using System.Security.Cryptography;
using System.Security.Cryptography.X509Certificates;
using System.Security.Permissions;
using System.Threading.Tasks;
using System.Configuration;
using System.Text.RegularExpressions;
using EnglishRemix.Services.Model;
using log4net;
using EnglishRemix.Services.Model.Exceptions;

namespace EnglishRemix.Services
{

    public partial class EnglishRemixBookService : IEnglishRemixBookService
    {

        [WebGet(UriTemplate = "user/?login_cred={login_cred}&login_route={login_route}&usr_pwd={usr_pwd}", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public String user_get(string login_cred, string login_route, string usr_pwd)
        {
            try{
                validateDevice(login_cred);
            }catch (DeviceValidationException e){
                return e.getFailedValidationResponse().toJson();
            }

            return "login_cred:" + login_cred + ", login_route:" + login_route + ", usr_pwd:" + usr_pwd;
        }

        /*
         * Add a new user.
         * id: third party id.
         * route: login route.
         */
        [WebInvoke(UriTemplate = "user/", Method = "PUT", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public String user_post(Stream input)
        {
            // Get query data
            var queryArray = getQueryArray(input);

            Response response = userFacade.setUser(queryArray["login_cred"], queryArray["login_route"], queryArray["usr_pwd"]);

            return response.toJson();
        }

        /*
         * Return user info.
         * id: third party id.
         * route: login route.
         */
        [WebInvoke(UriTemplate = "user/", Method = "POST", RequestFormat = WebMessageFormat.Json, ResponseFormat = WebMessageFormat.Json)]
        public String user_put(Stream input)
        {
            var queryArray = getQueryArray(input);

            String login_cred = queryArray["login_cred"];
            String login_route = queryArray["login_route"];
            String usr_pwd = queryArray["usr_pwd"];

            try{
                validateDevice(queryArray["deviceId"]);
            }catch (DeviceValidationException e){
                return e.getFailedValidationResponse().toJson();
            }

            // If the user logs in with a third party credential, it is at this stage deemed to be a valid one and do not need to be verified. It is already been done at the mobile client end.
            // What's needs to be done is to create an account if it does not already exist.
            if (login_route == "1" || login_route == "2")
            {
                // Query to see if the user exists. If not, create one, otherwise return the user.
                Response user_existence_response = userFacade.getUser(login_cred, login_route, usr_pwd);

                if (user_existence_response.result == "200")
                {
                    // User exists. Return user data.
                    return user_existence_response.toJson();
                }
                else
                {
                    // User doesn't exist. Create one now.
                    Response user_creation_response = userFacade.setUser(login_cred, login_route, usr_pwd);
                }
            }

            Response response = userFacade.getUser(login_cred, login_route, usr_pwd);

            return response.toJson();
        }


    }
}
